package cn.tedu.autoshow.website.service.impl;

import cn.tedu.autoshow.commons.ex.ServiceException;
import cn.tedu.autoshow.commons.pojo.vo.PageData;
import cn.tedu.autoshow.commons.util.PageInfoToPageDataConverter;
import cn.tedu.autoshow.commons.web.ServiceCode;
import cn.tedu.autoshow.website.mapper.BannerMapper;
import cn.tedu.autoshow.website.pojo.entity.Banner;
import cn.tedu.autoshow.website.pojo.param.BannerAddNewParam;
import cn.tedu.autoshow.website.pojo.param.BannerUpdateInfoParam;
import cn.tedu.autoshow.website.pojo.vo.BannerListItemVO;
import cn.tedu.autoshow.website.pojo.vo.BannerStandardVO;
import cn.tedu.autoshow.website.pojo.vo.TicketListListItemVO;
import cn.tedu.autoshow.website.service.IBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class BannerServiceImpl implements IBannerService {

    @Autowired
    BannerMapper mapper;

    @Override
    public void addNew(BannerAddNewParam bannerAddNewParam) {
        log.info("【开始】:【新增—轮播图】");
        log.debug("传入参数, bannerAddNewParam={}", bannerAddNewParam);
        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerAddNewParam, banner);
        banner.setGmtCreate(LocalDateTime.now())
                .setGmtModified(LocalDateTime.now());

        log.debug("整合实体, banner={}", banner);
        int insert = mapper.insert(banner);
        log.info("【结果1】:插入数据结果, insert = {}", insert);
    }

    @Override
    public void deleteById(Long id) {
        log.info("【开始】:【根据ID删除—轮播图】");
        log.debug("传入参数, id={}", id);
        //检查轮播图ID检查轮播图是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Banner> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", id);
        int countById = mapper.selectCount(queryWrapper2);
        log.info("规则：根据轮播图id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据轮播图id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "删除轮播图失败，轮播图数据不存在！");
        }


        int delete = mapper.deleteById(id);
        log.info("【结果1】:删除数据结果, delete = {}", delete);
    }

    @Override
    public void updateInfoById(Long id, BannerUpdateInfoParam bannerUpdateInfoParam) {
        log.info("【开始】:【根据ID更新—轮播图】");
        log.debug("传入参数, id={}, bannerAddNewParam={}", id, bannerUpdateInfoParam);

        //检查轮播图ID检查轮播图是否存在数据，如果不存在，则抛出异常
        QueryWrapper<Banner> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("id", id);
        int countById = mapper.selectCount(queryWrapper2);
        log.info("规则：根据轮播图id检索数据库结果,countById={}", countById);
        if(countById == 0){
            log.info("结果2:根据轮播图id检索数据量为0，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "编辑轮播图失败，轮播图数据不存在！");
        }

        Banner banner = new Banner();
        BeanUtils.copyProperties(bannerUpdateInfoParam, banner);
        banner.setId(id);
        log.debug("整合实体, banner={}", banner);
        int update = mapper.updateById(banner);
        log.info("【结果1】:更新数据结果, update = {}", update);
    }

    @Override
    public BannerStandardVO getStandardById(Long id) {
        log.info("开始:【根据ID查询-轮播图】");
        log.debug("传入参数,id={}", id);
        BannerStandardVO standardById = mapper.getStandardById(id);

        //根据轮播图ID检查轮播图是否存在数据，如果不存在，则抛出异常
        if(standardById==null){
            log.info("结果2:根据轮播图id查询详情，结果为null，抛出异常...");
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, "查询轮播图详情失败，轮播图数据不存在");
        }

        log.info("结果1:查询出数据,standardById");
        return standardById;
    }

    @Override
    public PageData<BannerListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum, pageSize);
    }

    @Override
    public PageData<BannerListItemVO> list(Integer pageNum, Integer pageSize) {
        log.info("开始:【查询-轮播图-分页】");
        log.debug("传入参数, pageNum={}, pageSize={}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<BannerListItemVO> list = mapper.list();
        PageInfo<BannerListItemVO> pageInfo = new PageInfo<>(list);
        PageData<BannerListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        log.debug("结果1: 查询结果：pageData={}", pageData);
        return pageData;
    }

    @Override
    public PageData<BannerListItemVO> listByTypeId(Integer typeId, Integer pageNum){
        Integer pageSize = 5;
        return listByTypeId(typeId, pageNum, pageSize);
    }

    @Override
    public PageData<BannerListItemVO> listByTypeId(Integer typeId, Integer pageNum, Integer pageSize){
        log.info("开始:【查询-轮播图-分页】");
        log.debug("传入参数, pageNum={}, pageSize={}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<BannerListItemVO> list = mapper.listByTypeId(typeId);
        PageInfo<BannerListItemVO> pageInfo = new PageInfo<>(list);
        PageData<BannerListItemVO> pageData = PageInfoToPageDataConverter.converter(pageInfo);
        log.debug("结果1: 查询结果：pageData={}", pageData);
        return pageData;
    }
}
